/**
 * Related Elements Grid 
 * @author Kirill Egorov 2011
 * @var {Ext.Panel}
 */

Ext.define('app.relatedGridModel', {
    extend: 'Ext.data.Model',
    fields: [
     	{name:'id' , type:'integer'},
        {name:'published' , type:'boolean'},
        {name:'deleted' , type:'boolean'},
        {name:'title' , type:'string'}
    ]
});

Ext.define('app.relatedGridPanel',{
	extend:'Ext.Panel',
	dataUrl:false,
	dataGrid:null,
	dataStore:null,
	fieldName:null,
	
	constructor: function(config) {
		config = Ext.apply({
			   layout:'fit',
			   autoScroll:false,
			   border:false,
			   tbar:[]
		}, config);				
		this.callParent(arguments);	
	},
	
	initComponent:function(){
		this.dataStore = Ext.create('Ext.data.Store',{
				model:'app.relatedGridModel',
				proxy: {
				        type: 'ajax',
				        reader: {
				            type: 'json',
				            idProperty: 'id'
				        }
				},
				autoLoad:false
		});
		
		this.dataGrid = Ext.create('Ext.grid.Panel' ,{
			store: this.dataStore,
			viewConfig:{
		 		stripeRows:false
		 	},
            frame: false,
            loadMask:true,
		    columnLines: true,
		    autoScroll:true,
		    enableHdMenu:false,
		    columns: [
					{
					    sortable: false,
					    text: appLang.STATUS,
					    dataIndex: 'published',
					    width:50,
					    align:'center',
					    renderer:function(value, metaData, record, rowIndex, colIndex, store){
					    	if(record.get('deleted')){
					    		 metaData.attr = 'style="background-color:#000000;white-space:normal;"';
					    		 return '<img src="/i/system/trash.png" data-qtip="'+appLang.INSTANCE_DELETED+'" >';
					    	}else{
					    		return app.publishRenderer(value, metaData, record, rowIndex, colIndex, store);
					    	}
					    }
		             },{
						id: 'title',
						sortable: false,
					    text: appLang.TITLE,
					    flex:1,
					    dataIndex: 'title'
					},
					app.sotrColumn 
			 ]
		});
		
		this.tbar=[
			{
				text:appLang.ADD_ITEM,
				listeners:{
					'click':{
							fn:function(){
								this.fireEvent('addItemCall');
							},
							scope:this
					}
				}
			}
		];
		
		this.items=[this.dataGrid];	
		this.callParent(arguments);	
		this.addEvents(
            /**
             * @event addItemCall
             */
           'addItemCall'
		);  
	},
	setData: function(data){
		this.dataStore.removeAll();
		this.dataStore.loadData(data);
	},
	addRecord:function(record){

		if(this.dataStore.findExact('id',record.get('id'))!=-1){
			 return;
		}
		
		var rPubblished = true;

		if(record.get('published')!=undefined){

			rPubblished = record.get('published');
		}

		var r = Ext.create('app.relatedGridModel', {
             id: record.get('id'),
             title:record.get('title'),
             deleted:0,
             published:rPubblished
         });

		this.dataStore.insert(this.dataStore.getCount(), r);

	},
	collectData: function(){
		var recordList = [];
		this.dataStore.each(function(record){
				if(!record.get('deleted'))
					recordList[recordList.length] = record.get('id');
		});
		var result = {};
		result[this.fieldName+'[]'] = recordList;
		return result;
	}
});